package com.kee.server.job;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kee.common.util.JSoupUtil;
import com.kee.server.service.ExamServiceImpl;
import com.kee.server.service.IExamService;
import com.kee.share.bean.Exam;
//七年级上学期有理数
//七年级上学期正数的加减
//一元二次方程
public class ExamJob extends Thread {
	private final static String USER_AGTNT = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 SE 2.X MetaSr 1.0";
	
	private final static IExamService service = new ExamServiceImpl();
	
//	private String baseURL = "http://www.jyeoo.com/math/ques/search?f=0&s=0&t=1&q=8d1aaadf-4eec-4c53-8b17-48dfc8288f4a%7e581be0c3-e629-4f1b-b065-f8dd2d9305db%7e";
	//private String baseURL = "http://www.jyeoo.com/math/ques/search?f=0&s=0&t=1&q=8d1aaadf-4eec-4c53-8b17-48dfc8288f4a%7e4a1834c8-8567-4b08-a863-61146ea07248%7e";
	
	//private final String baseURL = "http://www.jyeoo.com/math/ques/search?f=1&s=0&t=1&q=9";
	private final String baseURL = "http://www.jyeoo.com/math/ques/search?f=1&s=0&t=1&q=%26";
	private String grade = "初中";
	private String subject = "数学";
	private int pageNo = 24;
	
	@Override
	public void run() {
		for (int i =1 ; i <= pageNo; i++) {
			Document doc = null;
			try {
				doc = Jsoup.connect(baseURL+"&p="+i).userAgent(USER_AGTNT).timeout(5000).get();
			} catch (IOException e1) {
				e1.printStackTrace();
				continue;
			}
			Element element = doc.getElementById("page");
			Elements links = element.getElementsByClass("fieldtip");
			int j = 0;
			for (Element link : links) {
				//获取每个链接
				Element href = link.getElementsByTag("a").first();
				String a = href.attr("href");
				try {
					Exam exam = JSoupUtil.qingyou(USER_AGTNT, a);
					if (exam != null) {
						exam.setGrade(grade);
						exam.setSubject(subject);
						exam.setType(1);
						exam.setWebsite(1);
						exam.setSection("数学竞赛");
						service.add(exam);
					}
				} catch (Exception e) {
					System.out.println("pagNo:" + i);
					e.printStackTrace();
				}
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				j++;
				System.out.println("jingyou===PageNo:" + i + "===Size:" + j);
			}
		}
		
	}
	
	public static void main(String[] args) {
		ExamJob job = new ExamJob();
		job.start();
		TikuExamJob tj = new TikuExamJob();
		tj.start();
	}
}
